//

import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    goodsImgnum: wx.getStorageSync('picLimit')['gds_cover_num'] || 6,
    contentImgsnum: wx.getStorageSync('picLimit')['gds_content_num'] || 20,
    editorContent: {},
    intro: {},
    address: '',
    checked: false,
    supplyItem: {}, //供应商
    goodGroup: {}, //商品组
    goodCatItem: {}, //商品分类
    brandItem: {}, //品牌
    cententDetail:{ 
      agePrice:'',
      stoPrice:'',
      purchase:1
    },
    purchase: true,
    provIndex:'',
    cityIndex:'',
    provList:[],
    cityList:[],
    isHide:false,
    cityItem:[],
    cityIds:[], 
    
  },


  onLoad: function(options) {
    this.getProvlist();
    if (options && options.id) {
      this.setData({
        id: options.id
      })
      this.getdetail();
      this.getSaleAreaList()
      // this.getImg()
    }else{
      this.getSupplierList()
    }
    this.coverImgs = this.selectComponent("#coverImgs");
    this.contentImgs = this.selectComponent("#contentImgs");
    console.log(this.contentImgs)
    this.coverVideo = this.selectComponent("#coverVideo");
    this.contentVideo = this.selectComponent("#contentVideo")  
  },

  onShow() {
    // this.reque()
    this.findOpenCity();  
    console.log(this.data.cityItem)
  },

  //选择商品图片
  chooseGoodsImg: function(e) {   
    this.setData({
      imgs: this.coverImgs.data.imgs,
      isHide:true
    })
  },

  deleteGoodsImg: function(e) {
    this.setData({
      imgs: this.coverImgs.data.imgs
    })
  },

  deloriginGoodsImg: function() {
    this.setData({
      originImgs: this.coverImgs.data.originImgs
    })
  },


  completefun:function(){ 
    this.setData({
      isHide:false
    })
  },

  //选择介绍图片
  choosecontentImgs: function(e) { 
    this.setData({
      imgs: this.contentImgs.data.imgs
    })
  },

  deletecontentImgs: function(e) {
    this.setData({
      imgs: this.contentImgs.data.imgs
    })
  },

  delorigincontentImgs: function() {
    this.setData({
      originImgs: this.contentImgs.data.originImgs,
    })
  },



  //封面视频回调
  coverVideoCallback: function() {
    this.setData({
      coverVideo: this.coverVideo.data.video,
      coverVideoTime: '00:' +this.coverVideo.data.duration 
    })
    console.log(this.coverVideo.data.video, 'back')
  },

  //介绍视频回调
  contentVideoCallback: function() {
    this.setData({
      contentVideo: this.contentVideo.data.video,
      contentVideoTime: '00:'+this.contentVideo.data.duration 
    })
  },


  handleChoose: function(e) {
    var params = e.currentTarget.dataset 
    wx.navigateTo({
      url: '/pages/publish/template/commonlist?apiurl=' + params.apiurl + '&name=' + params.name + '&itemName=' + params.itemname + '&addUrl=' + params.addurl
    })
  },


  handlebrandChoose: function (e) {
    var params = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/publish/template/brandlist?apiurl=' + params.apiurl + '&name=' + params.name + '&itemName=' + params.itemname + '&addUrl=' + params.addurl + '&catid=' + params.catid
    })
  },

  //商品组
  // reque: function() {
  //   var that = this 
  //   _wxRequest.wxTonkenAjax({
  //     url: app.globalData.apiUrl + "/org/protected/comm/beanCrud/org.GoodsGroupVO/find",
  //     method: "post",
  //     data: {
  //       keyword: that.data.keywords,
  //       status: 1,
  //       pageSize: 10000
  //     }
  //   }, function(res) {
  //     console.log(res)

  //   })
  // },

  goback: function() {
    wx.navigateBack({

    })
  },
  //表单提交
  formSubmit: function(e) {
    var formdata = e.detail.value;
    formdata.supId = this.data.supplyItem.id;
    formdata.supName = this.data.supplyItem.supName;
    formdata.groupId = this.data.goodGroup.id,
    formdata.contentVideo = this.data.contentVideo
    formdata.coverVideo = this.data.coverVideo
    formdata.coverImgs = this.coverImgs.data.datalist

    formdata.coverVideoTime = this.data.coverVideoTime 
    formdata.contentVideoTime = this.data.contentVideoTime

    formdata.contentImgs = this.contentImgs.data.datalist
    formdata.catId = this.data.goodCatItem.id
    formdata.purchase = formdata.purchase?1:0
    formdata.present2b = formdata.present2b ? 1 : 0
    formdata.saleAreaList=this.data.cityItem;

    formdata.braId = this.data.brandItem.id
    formdata.brandName = this.data.brandItem.brandName



    console.log(formdata,'formdata')


    if (!purchase){ 
      formdata.openPresent = formdata.openPresent ? 1 : 0
    }

    // if (this.data.prov && this.data.proId){
    //   formdata.prov = this.data.prov;
    //   formdata.proId = this.data.proId;
    // }

    // if (this.data.city && this.data.cityId) {
    //   formdata.city = this.data.city;
    //   formdata.cityId = this.data.cityId;
    // }


    console.log('formdata:', formdata)


    if (!formdata.catId || formdata.catId == '') {
      wx.showToast({
        icon: 'none',
        title: '请选商品分类',
      })
      return
    }


    if (!formdata.supId || formdata.supId == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择供应商',
      })
      return
    }

    // if (!formdata.groupId || formdata.groupId == '') {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '请选择商品组',
    //   })
    //   return
    // }


    if (!formdata.goodsName || formdata.goodsName == '') {
      wx.showToast({
        icon: 'none',
        title: '商品名称不能为空',
      })
      return
    }

    if (!formdata.skuName || formdata.skuName == '') {
      wx.showToast({
        icon: 'none',
        title: 'sku名称不能为空',
      })
      return
    }

    if (!formdata.price || formdata.price == '') {
      wx.showToast({
        icon: 'none',
        title: '价格不能为空',
      })
      return
    }

    // if (!formdata.agePrice || formdata.agePrice == '') {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '代理拿货价不能为空',
    //   })
    //   return
    // }

    // if (!formdata.stoPrice || formdata.stoPrice == '') {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '店铺拿货价不能为空',
    //   })
    //   return
    // }
    var purchase=this.data.purchase
    if (!purchase && (!formdata.agePercent || formdata.agePercent == '')) {
      wx.showToast({
        icon: 'none',
        title: '代理分成点不能为空',
      })
      return
    }


    if (!purchase && (!formdata.stoPercent || formdata.stoPercent == '')){
      wx.showToast({
        icon: 'none',
        title: '店铺分成点不能为空',
      })
      return
    }

    if (!purchase && (!formdata.sharedPercent || formdata.sharedPercent == '')) {
      wx.showToast({
        icon: 'none',
        title: '分享分成点不能为空',
      })
      return
    }

    if (formdata.coverImgs && formdata.coverImgs.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '请上传商品轮播图片',
      })
      return
    }

    if (formdata.contentImgs && formdata.contentImgs.length < 1) {
      wx.showToast({
        icon: 'none',
        title: '请上传商品介绍图片',
      })
      return
    }

    this.formRequest(formdata)
  },
 

  // 请求
  formRequest: function(formdata) {

    var url = '/gds/protected/gdsMgt/createGoods';
    var title = "发布成功"
    //修改
    if (this.data.id) {
      url = "/gds/protected/gdsMgt/updateGoods";
      formdata.id = this.data.cententDetail.id;
      title = "修改成功"
    }
  
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/publish/goods/list',
        })
      }, 1500)

    })
  },

 
  //获取详情
  getdetail: function() {
    var that = this
    _wxRequest.wxAjax({
      // url: app.globalData.apiUrl + '/gds/protected/comm/beanCrud/gds.GoodsVO/getById/' + this.data.id,
      url: app.globalData.apiUrl + '/gds/goods/detail2?id=' + this.data.id,
      method: 'post'
    }, (res) => {

      if (res && res.vo) {
        var data = res.vo
        console.log(data, 'data')


      // 获取省份
        var provList = this.data.provList
        var provIndex = ''; 
        provList.map((item, index) => {
          if (item.id == data.proId) {
            provIndex = index 
          }
        })
 
       //图片
        this.coverImgs.setData({
          datalist: data.coverImgs,
          originImgs: data.coverImgs,
        })

        var contentImgs = data.contentImgs;

        this.contentImgs.setData({
          datalist: contentImgs,
          originImgs: contentImgs,
        })

       

        that.setData({
          proId:data.proId,
          provIndex,
          prov:data.prov,
          cententDetail: data,
          contentVideo: data.contentVideo,
          coverVideo: data.coverVideo, 
          purchase: data.purchase,
          present2b: data.present2b,
          supplyItem: {
            supName: data.supName,
            id: data.supId
          },
          goodGroup: {
            groupName: data.groupName || '选择商品组名称',
            id: data.groupId
          },
          goodCatItem: {
            id: data.catId,
            catName: data.catName || '选择分类名称'
          },
          brandItem: {
            id: data.braId,
            brandName: data.brandName || '请选择品牌'
          }     
        })  

        //获取城市列表
        if (data.proId) {
          this.getCitylist()
        } 
        
        if (data.cityId){
          setTimeout(() => {
            var cityList = this.data.cityList
            var cityIndex = '';
            cityList.map((item, index) => {
              if (item.id == data.cityId) {
                cityIndex = index
              }
            })
            that.setData({
              cityId: data.cityId,
              cityIndex,
              city: data.city,
            })
          }, 1500) 
        }


      }
    })
  },


//获取轮播图和介绍图片
  getImg:function(){
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/comm/beanCrud/gds.GoodsImgVO/find',
      method: 'post',
      data:{
        gdsId: this.data.id
      }
    }, (res) => {
      var coverImgs = [], 
          contentImgs=[] 
      var list=res.list;
      list.map((item)=>{
        if (item.imgType==1){
          coverImgs.push(item)
        } else if (item.imgType==2){
          contentImgs.push(item) 
        }
      })

      this.coverImgs.setData({
        datalist: coverImgs,
        originImgs: coverImgs, 
      })
      this.contentImgs.setData({
        datalist: contentImgs,
        originImgs: contentImgs, 
      })
    })
  },


  purchase:function(e){
    console.log(e,'val')
    this.setData({
      purchase: e.detail.value
    })
  },

  //监听商城价格变化
  priceChage:function(e){
    console.log(e,'price')
    var val = e.detail.value  
    this.setData({
      price: val
    }) 
  },


  // 获取省份
  getProvlist:function(){ 
    var that = this
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/sys/district/findByPid',
      method: "post",
      data: {
        pid: 0,
        "pageNo": 1,
        "pageSize": 100
      }
    }, (res)=>{ 
      this.setData({
        provList:res.list
      })
    }) 
  },

//选择省份
  provChange: function (e) {
    var provIndex = e.detail.value;
    var provList = this.data.provList
    var proId = '';
    var prov=''
    provList.map((item, index) => {
      if (index == provIndex) {
        proId = item.id
        prov=item.name
      }
    })

    this.setData({
      provIndex,
      proId,
      prov,
      cityIndex:'',
      cityId: '',
      city:''
    })
    this.getCitylist()
  },


  //选择城市 
  getCitylist: function () {
    var that = this
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/sys/district/findByPid',
      method: "post",
      data: {
        pid: this.data.proId,
        "pageNo": 1,
        "pageSize": 100
      }
    }, (res) => {
      this.setData({
       cityList: res.list
      })
    })
  },

  cityChange: function (e) {
    var cityIndex = e.detail.value;
    var cityList = this.data.cityList
    var cityId = ''; 
    var city=''
    cityList.map((item, index) => {
      if (index == cityIndex) {
        cityId = item.id,
        city=item.name
      }
    })

    this.setData({
      cityIndex,
      cityId,
      city
    })
  },



  // 获取列表
  getSupplierList: function () {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/bizOrg/findSupplierList',
      method: 'POST',
      data: { } 
    }, (res) => {
      var list = res.list
      if (list.length == 1) {
        that.setData({
          supplyItem: {
            id: list[0].id,
            supName: list[0].supName
          }
        })
      } 
    }) 
  },





  //选择城市 
  findOpenCity: function () {
    var that = this
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/sys/district/defaultCity',
      method: "post",
      data: { 
        "pageNo": 1,
        "pageSize": 10000000
      }
    }, (res) => {
      console.log(res,'resList')
    })
  },   


  //选择城市
  handleCtiy: function (e) {
    wx.setStorageSync('cityIds', '')
    var cityIds = e.currentTarget.dataset.item;
    console.log(e,'cityIds')
    wx.setStorageSync('cityIds', cityIds) 
    wx.navigateTo({
      url: '/pages/publish/template/district',
    })
  },



  // 获取列表
  getSaleAreaList: function () {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/goods/findSaleAreaList?gdsId='+this.data.id,
      method: 'POST',
      data: {}
    }, (res) => {
      console.log(res)
      var list=res.list
      var cityItem=[];

      list.map(item => {
        var obj={
          disId:item.disId,
          disName:item.disName
          }
        cityItem.push(obj)
      });


      var cityIds = list.map(item => item.disId);
      
      this.setData({
        cityItem,
        cityIds
      })
    })
  },


})